<?php
namespace App\Http\Controllers\Home;
use App\http\Controllers\Controller;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Session;

class ShopController extends BaseController
{
    //商品列表
    public function shop()
    {
        $this->go_login();
        $good = Db::table('good as g')
            ->leftJoin('reply as r', 'g.good_id', 'r.good_id')
            ->select('g.*', 'r.reply_score')
            ->get();
//        p($good);
        //赋值数组左边的键值是视图输出的变量
        //右边的变量是当前方法里面查询的数据
        return view('home/shop');
    }

    public function shop_list(Request $request)
    {
        $good_list = Db::table('good as g');
        if ($request->classify_id) {
            $good_list = $good_list->where("classify_id", $request->classify_id);
        }
        if ($request->color_name) {
            $colorName = Db::table('goodcolor')->where("color_name", $request->color_name)->pluck('good_id');

            $good_list = $good_list->whereIn("good_id", $colorName);
        }
        if ($request->good_size) {
            $sizeArr = Db::table('goodsize')->where("good_size", $request->good_size)->pluck('good_id');
            $good_list = $good_list->whereIn("good_id", $sizeArr);
        }
        if ($request->good_price) {
            $arr = explode('-', $request->good_price);
            $arr1 = trim($arr[0], '');
            $arr2 = trim($arr[1], '');
            $good_list = $good_list->whereRaw("good_price BETWEEN $arr1 AND $arr2");
        }
        $good_list = $good_list->paginate(12);

        foreach ($good_list->items() as $k => $v) {
            //图片
            $main_img = $v->main_img;
            $main_img = $main_img == '' ? '/static/home/img/product/10.jpg' : '/uploads/' . $main_img;
            $good_list->items()[$k]->main_img = $main_img;

            $good_name = $v->good_name;
            $good_name = msubstr($good_name,0,12,'utf-8',true);
            $good_list->items()[$k]->good_name = $good_name;

            //判断是否是新品/推荐
            $recommand = $v->recommand;
            $isNew = $v->isnew;
            if ($recommand == '1') { //判断是否是推荐
                $recommand = 'Sale';
            } else if ($isNew == '1') { //判断是否是新品
                $recommand = 'New';
            } else {
                $recommand = '';
            }
            $good_list->items()[$k]->recommand = $recommand;

            //判断类名
            $recommands = $v->recommand;;
            $isNew = $v->isnew;
            if ($recommands == 'Sale') { //判断是否是推荐
                $recommands = 'pro-label sale-label';
            } else if ($isNew == '1') { //判断是否是新品
                $recommands = 'pro-label new-label';
            } else {
                $recommands = '123';
            }
            $good_list->items()[$k]->isnew = $recommands;
        }
        foreach ($good_list->items() as $v) {
            echo '
               <div class="col-lg-3 col-md-4 col-sm-4 col-xs-12">
                            <div class="single-product">
                                <div class="product-img">
                                    <span class="' . $v->isnew . ' ">' . $v->recommand . '</span>
                                    <span class="pro-price-2">$' . $v->good_price . '</span>
                                    <a href="s_product/' . $v->good_id . '"><img style="height:270px" src="' . $v->main_img . '" alt="' . $v->main_img . '" /></a>
                                </div>
                                <div class="product-info clearfix text-center">
                                    <div class="fix">
                                        <h4 class="post-title"><a href="#">' . $v->good_name . '</a></h4>
                                    </div>
                                    <div class="fix">
													<span class="pro-rating">';

            $avgScore = Db::table('reply')->where('good_id', $v->good_id)->avg('reply_score');
            $avgScore = floor($avgScore);

            for ($i = 0; $i < 5; $i++) {
                if ($i + 1 > $avgScore) {
                    echo '<a ><i class="zmdi zmdi-star-outline"></i></a>';
                } else {
                    echo '<a href="javascript:;"><i class="zmdi zmdi-star"></i></a>';
                }
            }
//                                                            <a href="#"><i class="zmdi zmdi-star"></i></a>
//														<a href="#"><i class="zmdi zmdi-star"></i></a>
//														<a href="#"><i class="zmdi zmdi-star"></i></a>
//														<a href="#"><i class="zmdi zmdi-star-half"></i></a>
//														<a href="#"><i class="zmdi zmdi-star-half"></i></a>

            echo '</span>
                                    </div>
                                    <div class="product-action clearfix">
                                        <a href="javascript:;" data-toggle="tooltip" goodid="' . $v->good_id . '" id="wish" data-placement="top" title="Wishlist" class="';
            //是否是心愿单
            $iscookie = Cookie::has('u_name');
            if ($iscookie) {
                //接收cookie值
                $this->user_name = Cookie::get('u_name');
                $this->user_time = Cookie::get('u_time');
                //修改最后登录时间
                $data = array(
                    'user_addtime' => $this->user_time,
                );
                DB::table('user')->where("user_name", $this->user_name)->update($data);
                //重新设置cookie有效时间
                Cookie::queue('u_name', $this->user_name, 60);//60分钟
                Cookie::queue('u_time', time(), 60);//最后操作的时间

                $user_name = $this->user_name;
                $user_id = DB::table('user')->where('user_name', $user_name)->first();
                $user_id = $user_id->user_id;
                $wish = Db::table('wish')->where(['user_id' => $user_id, 'good_id' => $v->good_id])->first();
            } else {
                //删除session
                Session::forget('go_login');
                return redirect('/login');
            }
            if ($wish) {
                $color = 'wishColor';
            } else {
                $color = '1';
            }
            echo $color . ' "><i class="zmdi zmdi-favorite-outline"></i></a>
                                        <a href="#" data-toggle="modal"  goodid="' . $v->good_id . '"  id="modal" data-target="#productModal" title="Quick View"><i class="zmdi zmdi-zoom-in"></i></a>
                                        <a href="#" data-toggle="tooltip" data-placement="top" title="Compare"><i class="zmdi zmdi-refresh"></i></a>
                                        <a href="javascript:;" data-toggle="tooltip" goodid="' . $v->good_id . '" id="cart"  data-placement="top" title="Add To Cart" class="';
            //是否是加入购物车
            $iscookie = Cookie::has('u_name');
            if ($iscookie) {
                //接收cookie值
                $this->user_name = Cookie::get('u_name');
                $this->user_time = Cookie::get('u_time');
                //修改最后登录时间
                $data = array(
                    'user_addtime' => $this->user_time,
                );
                DB::table('user')->where("user_name", $this->user_name)->update($data);
                //重新设置cookie有效时间
                Cookie::queue('u_name', $this->user_name, 60);//60分钟
                Cookie::queue('u_time', time(), 60);//最后操作的时间

                $user_name = $this->user_name;
                $user_id = DB::table('user')->where('user_name', $user_name)->first();
                $user_id = $user_id->user_id;
                $shopcar = Db::table('shopcar')->where(['user_id' => $user_id, 'good_id' => $v->good_id])->first();
            } else {
                //删除session
                Session::forget('go_login');
                return redirect('/login');
            }
            if ($shopcar) {
                $cartColor = 'cartColor';
            } else {
                $cartColor = '1';
            }
            echo $cartColor . '"><i class="zmdi zmdi-shopping-cart-plus"></i></a>
                                    </div>
                                </div>
                            </div>
                </div>
        ';
        }
        echo '
                <div class="col-md-12">
                    <!-- Pagination start -->
                    <div class="shop-pagination  text-center">
                        <div class="pagination">
                           ' . $good_list->links('pagination::default') . '
                        </div>
                    </div>
                    <!-- Pagination end -->
                </div>
        ';
    }

    // 单数据
    public function goodid(Request $request)
    {
        $gooDid = Db::table('good')->where("good_id", $request->goodid)->first();
        echo json_encode($gooDid);
//       p($gooDid);
    }

    // 心愿单
    public function wish(Request $request)
    {
        $goodid = $request->goodid;
        $this->user_name = Cookie::get('u_name');
        $user_name = $this->user_name;
        $user_id = DB::table('user')->where('user_name', $user_name)->first();
        $user_id = $user_id->user_id;
        $data = array(
            'user_id' => $user_id,
            'wish_addtime' => time(),
            'good_id' => $goodid,
        );
        $wish = Db::table('wish')->insert($data);
        echo 1;
    }

    // 取消心愿单
    public function wish_del(Request $request)
    {
        $goodid = $request->goodid;
        $this->user_name = Cookie::get('u_name');
        $user_name = $this->user_name;
        $user_id = DB::table('user')->where('user_name', $user_name)->first();
        $user_id = $user_id->user_id;
        $data = array(
            'user_id' => $user_id,
            'good_id' => $goodid,
        );
        $wish = Db::table('wish')->where($data)->delete();
        echo 1;
    }

    // 购物车
    public function cart(Request $request)
    {
        $goodid = $request->goodid;
        $this->user_name = Cookie::get('u_name');
        $user_name = $this->user_name;
        $user_id = DB::table('user')->where('user_name', $user_name)->first();
        $user_id = $user_id->user_id;
        $data = array(
            'user_id' => $user_id,
            'number' => 1,
            'shopcar_addtime' => time(),
            'good_id' => $goodid,
            'goodcolor_id' => 1,
            'goodsize_id' => 1
        );
        $cart = Db::table('shopcar')->insert($data);
        echo 1;
    }

    // 取消购物车
    public function cart_del(Request $request)
    {
        $goodid = $request->goodid;
        $this->user_name = Cookie::get('u_name');
        $user_name = $this->user_name;
        $user_id = DB::table('user')->where('user_name', $user_name)->first();
        $user_id = $user_id->user_id;
        $data = array(
            'user_id' => $user_id,
            'good_id' => $goodid,
        );
        $cart = Db::table('shopcar')->where($data)->delete();
        echo 1;
    }

    // 分类
    public function get_classify()
    {
        $get_classify = Db::table('classify')->get();
        foreach ($get_classify as $v) {
            echo '
             <li><span classify="' . $v->classify_id . '">' . $v->classify_name . '</span></li>
        ';
        }
    }

    //颜色
    public function get_color()
    {
        $get_color = Db::table('goodcolor')->select('color_name', 'color')->distinct('color_name')->get();
        foreach ($get_color as $v) {
            $count = Db::table('goodcolor')->where('color_name', $v->color_name)->count();
            echo '
                 <li><span class="color" style="background:' . $v->color . ' "> </span><span class="color-name">' . $v->color_name . '</span><span class="count">' . $count . '</span></li>
            ';
        }
    }

    // 商品详情
    public function single_product($gid)
    {
        $this->go_login();
        $good_details = DB::table('good as g')
            ->select("g.*", "c.classify_name")
            ->leftJoin('classify as c', "g.classify_id", "c.classify_id")
            ->where("g.good_id", $gid)->first();
        $goodcolor = DB::table('goodcolor')->where("good_id", $gid)->get();
        $goodimg = DB::table('goodimg')->where("good_id", $gid)->get();
        $goodsize = DB::table('goodsize')->where("good_id", $gid)->get();
        $goods_reply = DB::table('reply')->where("good_id", $gid)->get();
        $wish = Db::table('wish')->where("good_id", $gid)->first();
        if ($wish) {
            $color = 'wishColor';
        } else {
            $color = '1';
        }
        //是否是加入购物车
        $shopcar = Db::table('shopcar')->where("good_id", $gid)->first();
        if ($shopcar) {
            $cartColor = 'cartColor';
        } else {
            $cartColor = '1';
        }
        //  echo $good_details->good_dec;die;
        // $good_details->good_dec=htmlspecialchars_decode($good_details->good_dec);
        // dd($goodcolor);
        $arr = array(
            'good_details' => $good_details,
            'goodcolor' => $goodcolor,
            'goodimg' => $goodimg,
            'goodsize' => $goodsize,
            'goods_reply' => $goods_reply,
            'wish' => $wish,
            'color' => $color,
            'shopcar' => $shopcar,
            'cartColor' => $cartColor,
        );

        return view('home/single_product', $arr);
    }

    //留言
    public function reply($rid)
    {
        $input = Request::all();
        $data = array(
            'reply_name' => $input['reply_name'],
            'reply_email' => $input['reply_email'],
            'reply_content' => $input['reply_content'],
            'good_id' => $rid,
            'reply_time' => time(),
        );
        // echo $rid;
        // dd($data);
        $res = DB::table('reply')->where("good_id", $rid)->insert($data);
        // dd($res);
        if ($res) { //成功
            show_msg("留言成功", "/s_product/{$rid}");
        } else {
            show_msg("数据执行有误，请重试", "/s_product/{$rid}");
        }
    }

    public function cart1(Request $request)
    {
        $goodid = $request->goodid;
        $number = $request->number;
        $goodsize = $request->goodsize;
        $goodcolor = $request->goodcolor;
        //是否是加入购物车
        $iscookie = Cookie::has('u_name');
        if ($iscookie) {
            //接收cookie值
            $this->user_name = Cookie::get('u_name');
            $this->user_time = Cookie::get('u_time');
            //修改最后登录时间
            $data = array(
                'user_addtime' => $this->user_time,
            );
            DB::table('user')->where("user_name", $this->user_name)->update($data);
            //重新设置cookie有效时间
            Cookie::queue('u_name', $this->user_name, 60);//60分钟
            Cookie::queue('u_time', time(), 60);//最后操作的时间

            $user_name = $this->user_name;
            $user_id = DB::table('user')->where('user_name', $user_name)->first();
            $user_id = $user_id->user_name == $user_name ? $user_id->user_id : '';

            $cart_arr = Db::table('shopcar')->whereRaw("good_id=$goodid AND user_id=$user_id")->first();
            if ($cart_arr) { //已经有该商品，编辑
                $data = array(
                    'number' => $number,
                    'goodsize_id' => $goodsize,
                    'goodcolor_id' => $goodcolor,
                );
                Db::table('shopcar')->whereRaw("good_id=$goodid AND user_id=$user_id")->update($data);
                return 2;
            } else {
                //添加
                $data = array(
                    'good_id' => $goodid,
                    'user_id' => $user_id,
                    'number' => $number,
                    'goodsize_id' => $goodsize,
                    'goodcolor_id' => $goodcolor,
                    'shopcar_addtime' => time(),
                );
                Db::table('shopcar')->insert($data);
                return 1;
            }
        }else {
            //删除session
            Session::forget('go_login');
            return redirect('/login');
        }
    }
}